package com.kehutong.charge.service.impl;

import com.kehutong.charge.entity.ChargeFeeConfig;
import com.kehutong.charge.service.ChargeFeeConfigService;
import com.kehutong.charge.util.enums.EnableStateEnum;
import com.kehutong.common.constant.CommonConstant;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.List;

/**
 * @program: kht
 * @description: 费用设置
 * @author: joneron
 * @create: 2021/6/29
 **/
public class ChargeFeeConfigServiceImpl implements ChargeFeeConfigService {

    @Inject
    private JdbcSession jdbcSession;



    @Override
    public Page<ChargeFeeConfig> findPage(JSONObject jsonObject) {
        this.initDefaultConfig();
        FindPage<ChargeFeeConfig> find = jdbcSession.findPage(ChargeFeeConfig.class)
                .eq("deleted", false)
                .eq(jsonObject,"communityNo")
                .like(jsonObject,"feeName")
                .eq(jsonObject,"status")
                .order("sort", "desc")
                .order("createTime", "desc");

        find.page(jsonObject);
        return find.exe();
    }


    private void initDefaultConfig(){
        ChargeFeeConfig feeConfig = jdbcSession.findOne(ChargeFeeConfig.class)
                //.eq("deleted", false) 需求变更，可删除
                .eq("communityNo", CommonConstant.DEFAULT_COMMUNITY_NO)
                .eq("initData", true)
                .exe();
        if(null != feeConfig){
            return;
        }
        ChargeFeeConfig chargeFeeConfig = Pool.newInstance(ChargeFeeConfig.class);
        chargeFeeConfig.setFeeName(CommonConstant.DEFAULT_COMMUNITY_NAME);
        chargeFeeConfig.setCommunityName(CommonConstant.DEFAULT_COMMUNITY_NAME);
        chargeFeeConfig.setCommunityNo(CommonConstant.DEFAULT_COMMUNITY_NO);
        chargeFeeConfig.setApproveMethod(EnableStateEnum.ENABLE);
        chargeFeeConfig.setApplyName("--");
        chargeFeeConfig.setSort(999);
        chargeFeeConfig.setInitData(true);
        chargeFeeConfig.setStatus(true);
        chargeFeeConfig.setCreateName("System");
        jdbcSession.insert(chargeFeeConfig);
    }


    @Override
    public List<ChargeFeeConfig> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(ChargeFeeConfig.class)
                .eq("deleted", false)
                .eq("status", true)
                .exe();
    }


    @Override
    public Object save(ChargeFeeConfig chargeFeeConfig) {
        if(chargeFeeConfig.isInitData()){
            throw new RuntimeException("系统初始化数据不可修改.");
        }
        List<ChargeFeeConfig> feeConfigs = jdbcSession.findArray(ChargeFeeConfig.class)
                .eq("communityNo",chargeFeeConfig.getCommunityNo()).exe();
        if(Objects.nonEmpty(feeConfigs)){
            return Result.error(123,"小区已经存在设置模板！");
        }

        return jdbcSession.insert(chargeFeeConfig);
    }


    @Override
    public Object update(ChargeFeeConfig chargeFeeConfig) {
        if(chargeFeeConfig.isInitData() && !CommonConstant.DEFAULT_COMMUNITY_NO.equals(chargeFeeConfig.getCommunityNo())){
            throw new RuntimeException("系统初始化数据不可修改.");
        }
        return jdbcSession.updateById(chargeFeeConfig);
    }


    @Override
    public Object deleteById(String id) {
        ChargeFeeConfig result = Pool.get(ChargeFeeConfig.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }

    @Override
    public ChargeFeeConfig getObj(JSONObject jsonObject) {
        return jdbcSession.findOne(ChargeFeeConfig.class).eq(jsonObject,"communityNo").exe();
    }
}
